/*
  This file is part of Open-Doors.

  Open-Doors is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.
  
  Open-Doors is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with Open-Doors.  If not, see <http://www.gnu.org/licenses/>.

  Authors: Rune Thorbek & Araz B. Makoo
*/ 

var kampagne_win;

function renderTip(val, meta, rec, rowIndex, colIndex, store) {
    meta.tdAttr = 'data-qtip="' + val.replace(new RegExp('\ ', 'g'), "&nbsp") + '"';
    return val;
}

function showKampagneForm(title, edit, data) {
    if (!kampagne_win) {
        var form = Ext.widget('form', {
            layout: {
                type: 'vbox',
                align: 'stretch'
            },
            border: false,
            bodyPadding: 10,
	    
            fieldDefaults: {
                labelAlign: 'top',
                labelWidth: 100,
                labelStyle: 'font-weight:bold',
		labelSeparator: "" 
            },
                defaults: {
                    margins: '0 0 0 0'
                },
	    
            
	    items: [{
		xtype: 'textfield',
		id: 'form_kampagne_edit',
                fieldLabel: 'edit',
		hidden: true
	    }, {
                xtype: 'textfield',
		id: 'form_kampagne_uid',
                fieldLabel: 'uid',
		hidden: true
	    },{
                xtype: 'textfield',
		id: 'form_kampagne_navn',
                fieldLabel: 'Navn*',
		allowBlank: false
	    }],
	    
	    buttons: [{
                text: 'Ok',
                handler: function() {
                    if (this.up('form').getForm().isValid()) {
			commit_kampagne();
                        this.up('form').getForm().reset();
                        this.up('window').hide();
		    }
                }
            }, {
                text: 'Annuller',
                handler: function() {
                    this.up('form').getForm().reset();
                    this.up('window').hide();
                }
            }]
        });
	
        kampagne_win = Ext.widget('window', {
            title: title,
            closeAction: 'hide',
            width: 400,
            height: 150,
            layout: 'fit',
            resizable: false,
            modal: true,
            items: form
        });
    }

    kampagne_win.down('form').getForm().reset();
    if (edit) {
	Ext.getCmp('form_kampagne_edit').setValue(edit);
	Ext.getCmp('form_kampagne_uid').setValue(data.get('uid'));
	Ext.getCmp('form_kampagne_navn').setValue(data.get('name'));
    }

    kampagne_win.setTitle(title);
    kampagne_win.show();
}


function commit_kampagne() {
  
    var edit_value = Ext.getCmp('form_kampagne_edit').getValue();
    var uid_value = Ext.getCmp('form_kampagne_uid').getValue();
    var navn_value = Ext.getCmp('form_kampagne_navn').getValue();
    
    Ext.Ajax.request({
	url: 'db_functions.php',
	method: 'post',
	params: {
	    function  : 'commit_campaign',
	    edit: edit_value,
	    uid: uid_value,
	    name: navn_value
	},
	success: function(response, object) {
	    var res = Ext.decode(response.responseText);
	 
            if (res.success == false){
		Ext.MessageBox.alert("Fejl", res.msg);
	    } else {
		var selection = Ext.getCmp("kampagner_grid").getSelectionModel().getSelection();
		
		if (selection.length != 0) {
		    selection[0].data = res.results[0]
		    var index = Ext.getCmp("kampagner_grid").getStore().indexOf(selection[0]);
		    Ext.getCmp("kampagner_grid").getView().refreshNode(index);
		}
		//Ext.MessageBox.alert("Info", res.msg);
	    }
	    if (edit_value == null || edit_value == '') {
		search("kampagner");
	    }
	}
    });
}

function edit_kampagne(data) {
    showKampagneForm('Rediger kampagne', true, data);
}

var contextmenu_kampagner = new Ext.menu.Menu({
    items: [
	{ 
	    text: 'Rediger',
	    icon: 'app/view/edit_row.gif',
	    handler: function() {
		var selection = Ext.getCmp("kampagner_grid").getSelectionModel().getSelection();
		if (selection.length > 0) {
		    edit_kampagne(selection[0]);
		}
	    }
	}, { 
	    text: 'Slet',
	    icon: 'app/view/delete_row.gif',
	    id: 'delete_kampagne_menuitem',
	    hidden: true,
	    handler: function() {
		var selection = Ext.getCmp("kampagner_grid").getSelectionModel().getSelection();
		if (selection.length > 0) {
		    Ext.Msg.confirm('Spørgsmål', 'Er du sikker på, at du vil slette rækken?', function(btn, text){
			if (btn == 'yes'){
			    delete_row("kampagner", "delete_campaign", selection[0].get('uid'));
			    return;
			}
		    });
		}
	    }
	}
    ]
});

Ext.define('OpenDoors.view.KampagnerView', {
    extend: 'Ext.panel.Panel',
    alias: 'widget.kampagneview',

    hideHeaders: true,
    

    items: [{
	xtype: 'component',
	html: '<font size="5">Kampagner</font>',
 	margin: "30 0 0 30"
	
    },
	    {
		xtype: 'container',
		width: 600,
		layout: 'hbox',
		border: false,
		items: [{
		    xtype: 'textfield',
		    id: 'search_kampagner_field',
		    width: 300,
		    margin: "50 0 0 30",
		    listeners:{  
			scope:this,  
			specialkey: function(f,e) {  
			    if(e.getKey()==e.ENTER) {  
				search("kampagner");
			    }  
			}  
		    }
		}, {
		    xtype: 'button',
		    width: 100,
		    text: 'Søg/Opdater',
		    id: 'search_kampagner_button',
		    margin: "50 0 0 10"
		}, {
		    xtype: 'button',
		    width: 100,
		    text: 'Tilføj...',
		    id: 'add_kampagne_button',
		    margin: "50 0 0 10"
		}]
	    }, {
		xtype: 'grid',
		id: 'kampagner_grid',
		columnLines: true,
		margin: "30 15 30 15",
		store: 'Kampagner',
		verticalScroller: {
		    xtype: 'paginggridscroller'
		},
		listeners: {
		    beforeitemcontextmenu: function(view, record, item, index, e)
		    {
			e.stopEvent();
			contextmenu_kampagner.showAt(e.getXY());
		    }
		},
				
		columns: [{
		    xtype: 'rownumberer',
		    sortable: false,
		    width: 50
		}, {
		    text: 'Navn',
		    dataIndex: 'name',
		    width: 240,
		    renderer: renderTip
		}, {
		    text: 'Antal indsamlere',
		    dataIndex: 'no_collectors',
		    align:'right', 
		    style: 'text-align:left',
		    flex: 1,
		    renderer: renderTip
		}, {
		    text: 'Beløb totalt',
		    dataIndex: 'total',
		    align:'right', 
		    style: 'text-align:left',
		    flex: 1,
		    renderer: renderTip
		}]
	    }],
    
    initComponent: function() {
        
        this.callParent();
    }
});

